package com.szewec.ps.base;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.szewec.ps.R;
import com.szewec.ps.myinterface.CallBackInterface;
import com.szewec.ps.myinterface.RightMenuClickEvent;
import com.szewec.ps.util.CommonUtils;

public class BaseTitleBar extends RelativeLayout {
    protected RelativeLayout leftLayout;
    protected ImageView leftImage;
    protected RelativeLayout rightLayout;
    protected ImageView rightImage;
    protected TextView rightText;
    protected TextView titleView;
    protected RelativeLayout titleLayout;
    protected TextView leftText;

    public BaseTitleBar(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public BaseTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public BaseTitleBar(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs){
        LayoutInflater.from(context).inflate(R.layout.widget_title_bar, this);
        leftLayout = findViewById(R.id.left_layout);
        leftImage = findViewById(R.id.left_image);
        rightLayout = findViewById(R.id.right_layout);
        rightImage = findViewById(R.id.right_image);
        titleView = findViewById(R.id.title);
        titleLayout = findViewById(R.id.root);
        rightText = findViewById(R.id.right_text);
        leftText = findViewById(R.id.left_text);

        parseStyle(context, attrs);
    }

    private void parseStyle(Context context, AttributeSet attrs){
        if(attrs != null){
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BaseTitleBar);
            String title = ta.getString(R.styleable.BaseTitleBar_titleBarTitle);
            titleView.setText(title);

            Drawable leftDrawable = ta.getDrawable(R.styleable.BaseTitleBar_titleBarLeftImage);
            if (null != leftDrawable) {
                leftImage.setImageDrawable(leftDrawable);
            }
            Drawable rightDrawable = ta.getDrawable(R.styleable.BaseTitleBar_titleBarRightImage);
            if (null != rightDrawable) {
                rightImage.setImageDrawable(rightDrawable);
            }

            Drawable background = ta.getDrawable(R.styleable.BaseTitleBar_titleBarBackground);
            if(null != background) {
                titleLayout.setBackgroundDrawable(background);
            }

            ta.recycle();
        }
    }

    public void setLeftImageResource(Drawable drawable) {
        leftImage.setImageDrawable(drawable);
    }

    public void setRightImageResource(Drawable drawable) {
        rightImage.setImageDrawable(drawable);
    }

    public void setLeftLayoutClickListener(CallBackInterface callBack){
        leftLayout.setOnClickListener(v -> callBack.customizedFun());
    }

    public void setRightLayoutClickListener(CallBackInterface callBack){
        rightLayout.setOnClickListener(v -> callBack.customizedFun());
    }

    public void setLeftLayoutClickListener(OnClickListener listener){
        leftLayout.setOnClickListener(listener);
    }

    public void setRightLayoutClickListener(OnClickListener listener){
        rightLayout.setOnClickListener(listener);
    }

    public void setRightTextClickListener(OnClickListener listener){
        rightText.setOnClickListener(listener);
    }

    public void setLeftLayoutVisibility(int visibility){
        leftLayout.setVisibility(visibility);
    }

    public void setRightLayoutVisibility(int visibility){
        rightLayout.setVisibility(visibility);
    }

    public void setRightTextVisibility(int visibility){
        rightText.setVisibility(visibility);
    }

    public void setRightImageVisibility(int visibility){
        rightImage.setVisibility(visibility);
    }

    public void setTitle(String title){
        titleView.setText(title);
    }

    public void setRightText(String mRightText){
        rightText.setText(mRightText);
    }

    public void setBackgroundColor(int color){
        titleLayout.setBackgroundColor(color);
    }

    public RelativeLayout getLeftLayout(){
        return leftLayout;
    }

    public RelativeLayout getRightLayout(){
        return rightLayout;
    }

    public ImageView getRightImage(){
        return rightImage;
    }

    public void hideLeftLayout(){
        leftLayout.setVisibility(View.GONE);
    }

    public void hideRightLayout(){
        rightLayout.setVisibility(View.GONE);
    }

    public void setLeftText(String text){
        leftText.setText(text);
    }
    public void setLeftImageVisibility(int visibility){
        leftImage.setVisibility(visibility);
    }

    /**
     * 通用查看模式:带更多按钮
     */
    public void setViewMode(Context context,RightMenuClickEvent rightMenuClickEvent){
        setRightTextVisibility(View.GONE);
        setRightImageResource(getResources().getDrawable(R.drawable.hole_more));
        setRightImageVisibility(View.VISIBLE);
        rightMenuClickEvent.switchMode(false);
        setRightLayoutClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtils.showMoreMenu(context, getRightLayout(), new CallBackInterface() {
                    @Override
                    public void customizedFun() {
                        rightMenuClickEvent.switchMode(true);
                        setEditMode(rightMenuClickEvent);
                    }
                }, new CallBackInterface() {
                    @Override
                    public void customizedFun() {
                        rightMenuClickEvent.doDelete();
                    }
                });
            }
        });

    }

    /**
     * 通用编辑模式:带保存按钮
     */
    public void setEditMode(RightMenuClickEvent rightClickEvent){
        setRightImageVisibility(View.GONE);
        setRightTextVisibility(View.VISIBLE);
        rightClickEvent.initEditMode();
        setRightLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rightClickEvent.doSave();
            }
        });
    }
}
